import { ModifierType } from './ModifierType';
import { ModifierSource } from './ModifierSource';
import { AttributeType } from './AttributeType';

export class AttributeModifier {
    // 定义万分比转换常量
    public static readonly PERCENTAGE_SCALE = 10000; // 万分比基数

    constructor(
        public readonly type: ModifierType,
        public readonly value: number, // 对于百分比类型，value直接存储万分比整数值
        public readonly source: ModifierSource,
        public readonly duration: number = -1, // -1 means permanent
        public readonly priority: number = 0,
        public readonly basedOnAttribute?: AttributeType // 基于哪个属性计算
    ) {}

    public apply(baseValue: number, basedOnValue?: number): number {
        switch (this.type) {
            case ModifierType.FLAT:
                return baseValue + this.value;
            case ModifierType.PERCENTAGE:
                // value已经是万分比整数值，直接使用
                return baseValue * (1 + this.value / AttributeModifier.PERCENTAGE_SCALE);
            case ModifierType.ATTRIBUTE_BASED:
                if (basedOnValue === undefined) {
                    return baseValue;
                }
                return baseValue + (basedOnValue * this.value);
            default:
                return baseValue;
        }
    }

    public remove(baseValue: number, basedOnValue?: number): number {
        switch (this.type) {
            case ModifierType.FLAT:
                return baseValue - this.value;
            case ModifierType.PERCENTAGE:
                // value已经是万分比整数值，直接使用
                return baseValue / (1 + this.value / AttributeModifier.PERCENTAGE_SCALE);
            case ModifierType.ATTRIBUTE_BASED:
                if (basedOnValue === undefined) {
                    return baseValue;
                }
                return baseValue - (basedOnValue * this.value);
            default:
                return baseValue;
        }
    }

    // 工具方法：将百分比转换为万分比整数值
    public static toPercentageValue(percentage: number): number {
        return Math.round(percentage * AttributeModifier.PERCENTAGE_SCALE);
    }

    // 工具方法：将万分比整数值转换为百分比
    public static fromPercentageValue(value: number): number {
        return value / AttributeModifier.PERCENTAGE_SCALE;
    }
} 